package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.FeedbackReq;
import com.yishusmt.mobsmt.dto.response.Tips;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.FeedbackEntity;
import com.yishusmt.mobsmt.enums.ResultCodeEnum;
import com.yishusmt.mobsmt.mapper.FeedbackEntityMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.FeedbackService;
import com.yishusmt.mobsmt.utils.DateUtil;
import com.yishusmt.mobsmt.utils.TipsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangyi
 * @date 2018/8/13 11:07
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackEntityMapper feedbackEntityMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 获取意见反馈接口
     *
     * @param req
     * @return
     */
    @Override
    public Result getFeedback(FeedbackReq req) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        //获取远程地址
        String remoteAddr = request.getRemoteAddr();
        UserRes userRes = userInfoMapper.selectUserSelective(req.getPhone());
        FeedbackEntity feedbackEntity = new FeedbackEntity();
        if(userRes != null) {
            feedbackEntity.setUserId(userRes.getId());
        }
        feedbackEntity.setContact(req.getContact());
        feedbackEntity.setContent(req.getContent());
        feedbackEntity.setVisitorIp(remoteAddr);
        feedbackEntity.setCreateTime(DateUtil.getCurrentDate());
        feedbackEntity.setUpdateTime(DateUtil.getCurrentDate());
        int i = feedbackEntityMapper.insertSelective(feedbackEntity);
        return 0==i?TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL,"反馈失败")
                : TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS,"我们正在处理");
    }
}
